package com.project.aiqicha;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.project.frame.common.annotation.LogIgnore;
import com.project.frame.common.controller.BaseController;
import com.project.frame.common.model.BaseResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * desc
 *
 * @author cjq
 * @date 2023-08-29
 */
@RestController
@RequestMapping("/aiqicha2")
@Slf4j
public class AiqichaController2 extends BaseController {


    static Map<String,String> headMap = new HashMap<>();

    static Map<String,String> headMap1 = new HashMap<>();
    static Map<String,String> headMap2 = new HashMap<>();
    static {
        headMap.put("Accept", " application/json, text/plain, */*");
        headMap.put("Accept-Language", " zh-CN,zh;q=0.9,zh-HK;q=0.8,en-US;q=0.7,en;q=0.6");
        headMap.put("Connection", " keep-alive");
        headMap.put("Cookie", " BIDUPSID=0D1DB03E0AF80F54AA480A6CEF29B5F3; PSTM=1690382464; BAIDUID=63FF48E469CEEDC77CCC2B161016CA48:FG=1; log_guid=6d9592f141772cab48b3be44fd24beed; _j47_ka8_=57; BDUSS=TBKT1Y2Nm01b1dVWnEzNEpBNmZCNVlESlRINDByMzVXU3VyN2Q0MFFVSmVrdkJrSVFBQUFBJCQAAAAAAAAAAAEAAABo1e2Nb25lX3BpZWNlMjAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAF4FyWReBclkW; BDUSS_BFESS=TBKT1Y2Nm01b1dVWnEzNEpBNmZCNVlESlRINDByMzVXU3VyN2Q0MFFVSmVrdkJrSVFBQUFBJCQAAAAAAAAAAAEAAABo1e2Nb25lX3BpZWNlMjAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAF4FyWReBclkW; BDPPN=69368a50b01596664992f080e3caf7d4; login_type=passport; _t4z_qc8_=xlTM-TogKuTw1Zgb6IIt3iDcTYuIkUiMYQmd; H_WISE_SIDS=234020_110085_259306_266566_267171_268592_268707_259642_269389_256151_269551_188333_269731_268236_269904_270646_270663_270966_271036_271022_268876_271119_271173_271178_267659_271320_271470_269610_270102_270875_271560_269777_271813_272077; H_WISE_SIDS_BFESS=234020_110085_259306_266566_267171_268592_268707_259642_269389_256151_269551_188333_269731_268236_269904_270646_270663_270966_271036_271022_268876_271119_271173_271178_267659_271320_271470_269610_270102_270875_271560_269777_271813_272077; BAIDUID_BFESS=63FF48E469CEEDC77CCC2B161016CA48:FG=1; ZFY=K7y:AyjX:Aenyt1BgPKNOfyl3D5IJBDi3W3B59X2ZswAs:C; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; _fb537_=xlTM-TogKuTw7eJ9-BrDjZ-67DhyeqXImQlhckYxAOuvmd; Hm_lvt_ad52b306e1ae4557f5d3534cce8f8bbf=1692949813,1693300439,1693314970,1693365151; Hm_lpvt_ad52b306e1ae4557f5d3534cce8f8bbf=1693365933; _s53_d91_=e27d644896d1800d6f2981fcb981e321adea0c9c207d962fd7fd1650b06e7c64d352812de00d5ecddceb68e28b0a0437582193fdb54f8d65b476f96cac38171e656f6bb4abb43ac9214b83daa2b5a3c04769bacc3446f5333975ccafb6ed40846be5e93d3b98495d41f5f92f5574c151cfc5f2f1ceb62c842964864d66631f529a0a1a84b2f338729257a0fe92e8ac61c0f1f82ae012d1c79212047cfd460088374f73784a544baf09bf7f75b55030bfb6e2d6b4004cf706714e5ea865d1f019b402f018e6610ac223a469cf1d90ab4a; _y18_s21_=b1adb267; log_first_time=1693376213963; log_last_time=1693376214501; RT=\"z=1&dm=baidu.com&si=8eca3fc6-b262-4b33-941e-2aba30339da0&ss=llx5vzpf&sl=d&tt=gx0&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf\"; ab169337520=185cafe1cdfe8cfea7def524bc24e8a616933762191d2");
        headMap.put("Host", " aiqicha.baidu.com");
        headMap.put("Referer", " https://aiqicha.baidu.com/");
        headMap.put("Sec-Fetch-Dest", " empty");
        headMap.put("Sec-Fetch-Mode", " cors");
        headMap.put("Sec-Fetch-Site", " same-origin");
        headMap.put("User-Agent", " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36");
        headMap.put("X-Requested-With", " XMLHttpRequest");
        headMap.put("Zx-Open-Url", " https://aiqicha.baidu.com/");
        headMap.put("sec-ch-ua", " \"Chromium\";v=\"116\", \"Not)A;Brand\";v=\"24\", \"Google Chrome\";v=\"116\"");
        headMap.put("sec-ch-ua-mobile", " ?0");
        headMap.put("sec-ch-ua-platform", " \"Windows\"");
        headMap.put("ymg_ssr", " 1693314118248_1693376219268_KmwJxb7YG3XjIjPr9Ufd3Aauww5pZ0F5Iwa/uojqFpVGnS9739zNZ3aNv/xHjx0Rb2k5Qd1QOVLaAPUnv5orV3VhR5AEcpuC/R9qGM7xKnWNHXvlQwdRXti0k1NCoPjGw/cQ+bNuSDXxKrNkFJbqWZnqUeFasFdYlEUdqoB5ZlD8J/J3M0T5lcK/u+9qlac7GTYMXbkm94LsTSGvt15xejBogdN+DX4KhLDr8jhCu6ttu87R9nxs36G1RW/UpEpZVw6/cPk/FL5Sq68ngAgy6VYwa1TdcyMgoqife5+WHsg=");
        headMap.put("Content-Type", "application/json");
    }

    static {
        headMap1.put("Accept", "application/json, text/plain, */*");
        headMap1.put("Accept-Language", "zh-CN,zh;q=0.9,zh-HK;q=0.8,en-US;q=0.7,en;q=0.6");
        headMap1.put("Connection", "keep-alive");
        headMap1.put("Cookie", "BIDUPSID=0D1DB03E0AF80F54AA480A6CEF29B5F3; PSTM=1690382464; BAIDUID=63FF48E469CEEDC77CCC2B161016CA48:FG=1; log_guid=6d9592f141772cab48b3be44fd24beed; _j47_ka8_=57; BDUSS=TBKT1Y2Nm01b1dVWnEzNEpBNmZCNVlESlRINDByMzVXU3VyN2Q0MFFVSmVrdkJrSVFBQUFBJCQAAAAAAAAAAAEAAABo1e2Nb25lX3BpZWNlMjAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAF4FyWReBclkW; BDUSS_BFESS=TBKT1Y2Nm01b1dVWnEzNEpBNmZCNVlESlRINDByMzVXU3VyN2Q0MFFVSmVrdkJrSVFBQUFBJCQAAAAAAAAAAAEAAABo1e2Nb25lX3BpZWNlMjAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAF4FyWReBclkW; BDPPN=69368a50b01596664992f080e3caf7d4; login_type=passport; _t4z_qc8_=xlTM-TogKuTw1Zgb6IIt3iDcTYuIkUiMYQmd; H_WISE_SIDS=234020_110085_259306_266566_267171_268592_268707_259642_269389_256151_269551_188333_269731_268236_269904_270646_270663_270966_271036_271022_268876_271119_271173_271178_267659_271320_271470_269610_270102_270875_271560_269777_271813_272077; H_WISE_SIDS_BFESS=234020_110085_259306_266566_267171_268592_268707_259642_269389_256151_269551_188333_269731_268236_269904_270646_270663_270966_271036_271022_268876_271119_271173_271178_267659_271320_271470_269610_270102_270875_271560_269777_271813_272077; BAIDUID_BFESS=63FF48E469CEEDC77CCC2B161016CA48:FG=1; ZFY=K7y:AyjX:Aenyt1BgPKNOfyl3D5IJBDi3W3B59X2ZswAs:C; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; Hm_lvt_ad52b306e1ae4557f5d3534cce8f8bbf=1692949813,1693300439,1693314970,1693365151; log_first_time=1693376213963; _fb537_=xlTM-TogKuTwk4vA%2A87zd9z0Gb-jP7d8UIK0LPyAiMhhmd; ab169337520=115cafe1cdfe8cfea7def524bc24e8a616933775984d8; ab169337880=145cafe1cdfe8cfea7def524bc24e8a616933789989d3; Hm_lpvt_ad52b306e1ae4557f5d3534cce8f8bbf=1693379000; ab_sr=1.0.1_MDUxMDljM2JmY2YzNDY4ODYxNGExNmUwOGY2MTU2ODk0NjY4ZTk4MmRlMDFmMDI2OGQ5ODBlMjhmZDU4YjhjNWVhMDE2YzdlMWYxYzgzZjczYmNlMjQxMTAzYWI0ZjhhODQ1ZTMyZGI5MGE4YjJkN2I0NTZmYmY1MjAzMWJjMzMxYjU3NmM1ZDFlYWMzOWFlODE2NjUwMzljMzVkNGFjOA==; _s53_d91_=e27d644896d1800d6f2981fcb981e321adea0c9c207d962fd7fd1650b06e7c64d352812de00d5ecddceb68e28b0a0437582193fdb54f8d65b476f96cac38171e656f6bb4abb43ac9214b83daa2b5a3c04769bacc3446f5333975ccafb6ed40846be5e93d3b98495d41f5f92f5574c151cfc5f2f1ceb62c842964864d66631f529a0a1a84b2f338729257a0fe92e8ac61c0f1f82ae012d1c79212047cfd460088374f73784a544baf09bf7f75b55030bf12bdeae3d39c5cee83288432784c5694902824b830029d7aa0a83f1ec6de40ec; _y18_s21_=c33222e8; RT=\"z=1&dm=baidu.com&si=8eca3fc6-b262-4b33-941e-2aba30339da0&ss=llx5vzpf&sl=x&tt=1c8h&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=8azkq&nu=9y8m6cy&cl=8ayg3\"; log_last_time=1693379100696");
        headMap1.put("Host", "aiqicha.baidu.com");
        headMap1.put("Referer", "https://aiqicha.baidu.com/s?city=%E5%8C%97%E4%BA%AC%E5%B8%82&f={%22provinceCode%22:[%22110000%22]");
        headMap1.put("Sec-Fetch-Dest", "empty");
        headMap1.put("Sec-Fetch-Mode", "cors");
        headMap1.put("Sec-Fetch-Site", "same-origin");
        headMap1.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36");
        headMap1.put("X-Requested-With", "XMLHttpRequest");
        headMap1.put("Zx-Open-Url", "https://aiqicha.baidu.com/s?city=%E5%8C%97%E4%BA%AC%E5%B8%82&f={%22provinceCode%22:[%22110000%22]");
        headMap1.put("sec-ch-ua", "\"Chromium\";v=\"116\", \"Not)A;Brand\";v=\"24\", \"Google Chrome\";v=\"116\"");
        headMap1.put("sec-ch-ua-mobile", "?0");
        headMap1.put("sec-ch-ua-platform", "\"Windows\"");
        headMap1.put("ymg_ssr", "1693314118248_1693379100699_KmwJxb7YG3XjIjPr9Ufd3Aauww5pZ0F5Iwa/uojqFpVGnS9739zNZ3aNv/xHjx0Rb2k5Qd1QOVLaAPUnv5orV3VhR5AEcpuC/R9qGM7xKnWNHXvlQwdRXti0k1NCoPjGw/cQ+bNuSDXxKrNkFJbqWZnqUeFasFdYlEUdqoB5ZlD8J/J3M0T5lcK/u+9qlac7BiH3Znr/GSTcrbqxi3RjO0Hy8Jmn/43Ia40KLKS8uOPPgDjBuHPxHsgubKX/yHs8I02QAoRHgqB/lqxpvWlTNN6Mz2CnG2QqwMW6umnRTkAxkkGfG568DbKrkVWhwqwaiKZNPx8XOaShGoIr27dab904bjYq4tCnuFTUEseRhWDQdxKK9Cww5E6WcXDAnaqbiVSBVYpW9CopfxaL5db4nw==");
    }

    static {
        headMap2.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
        headMap2.put("Accept-Language", "zh-CN,zh;q=0.9,zh-HK;q=0.8,en-US;q=0.7,en;q=0.6");
        headMap2.put("Cache-Control", "max-age=0");
        headMap2.put("Connection", "keep-alive");
        headMap2.put("Cookie", "BIDUPSID=0D1DB03E0AF80F54AA480A6CEF29B5F3; PSTM=1690382464; BAIDUID=63FF48E469CEEDC77CCC2B161016CA48:FG=1; log_guid=6d9592f141772cab48b3be44fd24beed; _j47_ka8_=57; BDUSS=TBKT1Y2Nm01b1dVWnEzNEpBNmZCNVlESlRINDByMzVXU3VyN2Q0MFFVSmVrdkJrSVFBQUFBJCQAAAAAAAAAAAEAAABo1e2Nb25lX3BpZWNlMjAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAF4FyWReBclkW; BDUSS_BFESS=TBKT1Y2Nm01b1dVWnEzNEpBNmZCNVlESlRINDByMzVXU3VyN2Q0MFFVSmVrdkJrSVFBQUFBJCQAAAAAAAAAAAEAAABo1e2Nb25lX3BpZWNlMjAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAF4FyWReBclkW; BDPPN=69368a50b01596664992f080e3caf7d4; login_type=passport; _t4z_qc8_=xlTM-TogKuTw1Zgb6IIt3iDcTYuIkUiMYQmd; H_WISE_SIDS=234020_110085_259306_266566_267171_268592_268707_259642_269389_256151_269551_188333_269731_268236_269904_270646_270663_270966_271036_271022_268876_271119_271173_271178_267659_271320_271470_269610_270102_270875_271560_269777_271813_272077; H_WISE_SIDS_BFESS=234020_110085_259306_266566_267171_268592_268707_259642_269389_256151_269551_188333_269731_268236_269904_270646_270663_270966_271036_271022_268876_271119_271173_271178_267659_271320_271470_269610_270102_270875_271560_269777_271813_272077; BAIDUID_BFESS=63FF48E469CEEDC77CCC2B161016CA48:FG=1; ZFY=K7y:AyjX:Aenyt1BgPKNOfyl3D5IJBDi3W3B59X2ZswAs:C; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; Hm_lvt_ad52b306e1ae4557f5d3534cce8f8bbf=1692949813,1693300439,1693314970,1693365151; log_first_time=1693376213963; _fb537_=xlTM-TogKuTwk4vA%2A87zd9z0Gb-jP7d8UAs9Y5Q4NLsCmd; ab169337520=165cafe1cdfe8cfea7def524bc24e8a616933775872d5; Hm_lpvt_ad52b306e1ae4557f5d3534cce8f8bbf=1693377588; ab_sr=1.0.1_OGY5YTY1NzBhOWUwNDgwOWUwOThiNWI5NTJkYjhjYjc1N2Y0MjU3ODMyOGUxZGY0ZTFkMjQzMTA4MjkzNmRjMTAxYjNmYWE0NjFlMDM3ZGE5YmRiODM4MGIzYTczYzBiMjQ4MGU2ZTk1OTg0ZDdmMTQyMjZhYzg5MmVmMTU5YjY0MjQ0ODBkOTgwYmYyNGY1MjJiNTExMTRmMDc5M2YzYQ==; _s53_d91_=e27d644896d1800d6f2981fcb981e321adea0c9c207d962fd7fd1650b06e7c64d352812de00d5ecddceb68e28b0a0437582193fdb54f8d65b476f96cac38171e656f6bb4abb43ac9214b83daa2b5a3c04769bacc3446f5333975ccafb6ed40846be5e93d3b98495d41f5f92f5574c151cfc5f2f1ceb62c842964864d66631f529a0a1a84b2f338729257a0fe92e8ac61c0f1f82ae012d1c79212047cfd460088374f73784a544baf09bf7f75b55030bf8bc7d0bd6428ea35179efd996b77c51fee8ba8610f7bc4a94257d19c7d325e29; _y18_s21_=64b5019f; log_last_time=1693377592365; RT=\"z=1&dm=baidu.com&si=8eca3fc6-b262-4b33-941e-2aba30339da0&ss=llx5vzpf&sl=j&tt=txm&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=7ep3v&ul=7esmo\"");
        headMap2.put("Host", "aiqicha.baidu.com");
        headMap2.put("Sec-Fetch-Dest", "document");
        headMap2.put("Sec-Fetch-Mode", "navigate");
        headMap2.put("Sec-Fetch-Site", "none");
        headMap2.put("Sec-Fetch-User", "?1");
        headMap2.put("Upgrade-Insecure-Requests", "1");
        headMap2.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36");
        headMap2.put("sec-ch-ua", "\"Chromium\";v=\"116\", \"Not)A;Brand\";v=\"24\", \"Google Chrome\";v=\"116\"");
        headMap2.put("sec-ch-ua-mobile", "?0");
        headMap2.put("sec-ch-ua-platform", "\"Windows\"");
    }
    static int idx = 0;

    @RequestMapping(value = "/detail/{id}")
    @LogIgnore
    public BaseResp detail(@PathVariable String id)
            throws Exception {

        String url = "https://aiqicha.baidu.com/company_detail_"+id;
        HttpResponse res = HttpUtil.createGet(url).headerMap(headMap,true).execute();
        if(res.isOk()){
            return BaseResp.renderSuccess(res.body());
        }
        return BaseResp.renderSuccess("");
    }

    @RequestMapping(value = "/search/{key}")
    @LogIgnore
    public BaseResp search(@PathVariable String key)
            throws Exception {

        long start = System.currentTimeMillis();
        String url = "https://aiqicha.baidu.com/index/districtListAjax";
        try{
            HttpResponse res = HttpUtil.createGet(url).headerMap(headMap,true).execute();

            idx++;
            if(res.isOk()){
                log.info("a.ok.count="+idx);
                String body = res.body();
                JSONObject bodyObj = JSONObject.parseObject(body);
                JSONArray data = bodyObj.getJSONArray("data");
                for(int i=0;i<data.size();i++){
                    JSONObject obj = data.getJSONObject(i);
                    String code = obj.getString("value");
                    String name = obj.getString("text");
                    advanceFilterAjax(code,name);
                    break;
                }
            }else{
                log.info("a.fail.count="+idx);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        log.info("diff="+(end-start));
        return BaseResp.renderSuccess("");
    }

    private void advanceFilterAjax(String code, String name) {
        for(int i=1;i<1000;i++){
            String url = "https://aiqicha.baidu.com/s/advanceFilterAjax";
            String params = "t=&p="+i+"&s=10&o=0&f={\"provinceCode\":[\""+code+"\"]}";
            url = url+"?"+params;
            HttpResponse res = HttpUtil.createGet(url).headerMap(headMap1,true).execute();
            if(res.isOk()){
                String body = res.body();
                JSONObject bodyObj = JSONObject.parseObject(body);
                JSONObject dataObj = bodyObj.getJSONObject("data");
                JSONArray resultList = dataObj.getJSONArray("resultList");
                openDetail(resultList);
            }
        }

    }

    private void openDetail(JSONArray detailList) {
        for (int i = 0; i < detailList.size(); i++) {
            JSONObject obj = detailList.getJSONObject(i);
            String pid = obj.getString("pid");
            String entName = obj.getString("entName");
            openByPid(pid,entName);
        }
    }

    private void openByPid(String pid,String entName) {
        String url = "https://aiqicha.baidu.com/company_detail_"+pid;
        HttpResponse res = HttpUtil.createGet(url).headerMap(headMap2,true).execute();
        idx++;
        if(res.isOk()){
            String body = res.body();
            if(body.contains(pid)){
                log.info("c.ok.entName="+entName+",count="+idx);
            }else{
                log.info("c.not.count="+idx);
            }
        }else{
            log.info("c.fail.count="+idx);
        }
    }

}
